package com.cashmanager.server.db;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import com.cashmanager.shared.vo.Cash;
import com.cashmanager.shared.vo.ValueObject;


public final class CashDaoImpl implements ICashDao {
	
	private static final Logger logger = Logger.getLogger(CashDaoImpl.class);
	private static final String GET_ALL_CASH = "select * from " + SCHEMA_NAME+".get_all_cash";
	@Override
	public  ArrayList<ValueObject> getAllCash() {
		Connection conn = null;
		try {
			conn = JDBCUtil.getInstance().getConnection();
			Statement stmt = conn.createStatement();
			logger.debug("Begin executing query for get all cash");
			ResultSet rs = stmt.executeQuery(GET_ALL_CASH);
			logger.debug("After executing query for get all cash");
			
			ArrayList<ValueObject> cashList = new ArrayList<ValueObject>();
			while (rs.next()){
				Cash cash = new Cash();
				int cashId = rs.getInt(1);
				String sequence = rs.getString(2);
				String uid  = rs.getString(3);
				String currencyCde = rs.getString(5);
				int faceValue = rs.getInt(6);
				String version = rs.getString(7);
				String cashierCde = rs.getString(8);
				String operatorCde = rs.getString(9);
				String branchCode = rs.getString(9);
				String cardNo = rs.getString(11);
				String resultCde = rs.getString(12);
				Date dateTime = rs.getDate(13);
				cash.setCashId(cashId);
				cash.setSequence(sequence);
				cash.setUid(uid);
				cash.setCurrencyCde(currencyCde);
				cash.setFaceValue(faceValue);
				cash.setVersion(version);
				cash.setCashierCde(cashierCde);
				cash.setOperatorCode(operatorCde);
				cash.setBranchCode(branchCode);
				cash.setCardNo(cardNo);
				cash.setResultCode(resultCde);
				cash.setDateTime(dateTime);
				cashList.add(cash);
			}
			logger.debug("Fetched " + cashList.size() + " cash from db.");
			return cashList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			JDBCUtil.closeConn(conn);
		}
		
		return null;
	}

}
